package com.duck.communication.controller;

import com.duck.common.domain.Result;
import com.duck.common.utils.UserContext;
import com.duck.communication.domain.dto.AnswerQuestionDto;
import com.duck.communication.domain.dto.SendQuestionDto;
import com.duck.communication.domain.vo.ExpertInfoVo;
import com.duck.communication.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 农户向指定专家提问
     *
     * @param questionPojo 问题
     * @return 状态码
     */
    @PostMapping("/sendDirectedQuestion/")
    public Result sendDirectedQuestion(@RequestBody SendQuestionDto questionPojo) {
        String userId = String.valueOf(UserContext.getUser());
        HashMap<String, Object> question = new HashMap<>() {{
            put("farmerId", userId);
            put("expertId", questionPojo.getExpertId());
            put("title", questionPojo.getTitle());
            put("content", questionPojo.getContent());
            put("image", questionPojo.getImage());
        }};
        Integer result = questionService.DirectedQuestion(question);
        if (result == 1) {
            return new Result(200, "发送成功");
        }
        return new Result(404, "发送失败");
    }

    /**
     * 农户向所有专家提问
     *
     * @param questionPojo 问题
     * @return 状态码
     */
    @PostMapping("/sendPublicQuestion/")
    public Result sendPublicQuestion(@RequestBody SendQuestionDto questionPojo) {
        String userId = String.valueOf(UserContext.getUser());
        HashMap<String, Object> question = new HashMap<>() {{
            put("farmerId", userId);
            put("title", questionPojo.getTitle());
            put("content", questionPojo.getContent());
            put("image", questionPojo.getImage());
        }};
        Integer result = questionService.PublicQuestion(question);
        if (result == 1) {
            return new Result(200, "发送成功");
        }
        return new Result(404, "发送失败");
    }

    /**
     * 查找自己问过的问题
     *
     * @return 问题列表
     */
    @GetMapping("/searchMyQuestion/")
    public Result searchMyQuestion() {
        String userId = String.valueOf(UserContext.getUser());
        List<HashMap<String, Object>> questionList = questionService.searchMyQuestion(userId);
        return new Result(questionList, 200, "查询成功");
    }

    /**
     * 返回所有的问题
     *
     * @return 问题列表
     */
    @GetMapping("/searchAllQuestions/")
    public Result searchAllQuestions() {
        List<HashMap<String, Object>> questionList = questionService.searchAllQuestions();
        return new Result(questionList, 200, "查询成功");
    }

    /**
     * 专家查找所有指定自己回答的问题
     *
     * @return 问题列表
     */
    @GetMapping("/searchDirectedQuestion/")
    public Result searchDirectedQuestion() {
        String userId = String.valueOf(UserContext.getUser());
        List<HashMap<String, Object>> questionList = questionService.searchDirectedQuestion(userId);
        return new Result(questionList, 200, "查询成功");
    }

    /**
     * 公共查找指定专家回答的所有问题
     *
     * @param expertId 专家id
     * @return 列表
     */
    @GetMapping("/PublicSearchDirectedQuestion/")
    public Result PublicSearchDirectedQuestion(@RequestParam String expertId) {
        List<HashMap<String, Object>> result = questionService.searchDirectedQuestion(expertId);
        return new Result(result, 200, "成功");
    }


    /**
     * 专家回答问题
     *
     * @param answerPojo 回答
     * @return 状态码
     */
    @PostMapping("/answerQuestion/")
    public Result answerQuestion(@RequestBody AnswerQuestionDto answerPojo) {
        String expertId = String.valueOf(UserContext.getUser());
        HashMap<String, Object> answer = new HashMap<>() {{
            put("userId", expertId);
            put("questionId", answerPojo.getQuestionId());
            put("content", answerPojo.getContent());
        }};
        Integer result = questionService.answerQuestion(answer);
        Integer result2 = questionService.updateIsAnswer(answerPojo.getQuestionId());
        if (result == 1 && result2 == 1) {
            return new Result(200, "成功");
        }
        return new Result(404, "失败");
    }

    /**
     * 查看对应专家回答过的问题
     *
     * @return 列表
     */
    @GetMapping("/searchExpertAnsweredQuestion/")
    public Result searchExpertAnsweredQuestion() {
        String expertId = String.valueOf(UserContext.getUser());
        List<HashMap<String, Object>> result = questionService.searchExpertAnsweredQuestion(expertId);
        return new Result(result, 200, "成功");
    }

    /**
     * 公共查找指定专家已回答的问题
     *
     * @param expertId 专家id
     * @return 列表
     */
    @GetMapping("/PublicSearchExpertAnsweredQuestion/")
    public Result PublicSearchExpertAnsweredQuestion(@RequestParam String expertId) {
        List<HashMap<String, Object>> result = questionService.searchExpertAnsweredQuestion(expertId);
        return new Result(result, 200, "成功");
    }

    /**
     * 查找指定专家但未回答的问题
     *
     * @return 列表
     */
    @GetMapping("/searchDirectedUnAnswer/")
    public Result searchDirectedUnAnswer() {
        String expertId = String.valueOf(UserContext.getUser());
        List<HashMap<String, Object>> result = questionService.searchDirectedUnAnswer(expertId);
        return new Result(result, 200, "成功");
    }

    /**
     * 公共查找指定专家但未回答的问题
     *
     * @param expertId 专家id
     * @return 列表
     */
    @GetMapping("/PublicSearchDirectedUnAnswer/")
    public Result PublicSearchDirectedUnAnswer(@RequestParam String expertId) {
        List<HashMap<String, Object>> result = questionService.searchDirectedUnAnswer(expertId);
        return new Result(result, 200, "成功");
    }

    /**
     * 查找所有已经回答过的问题
     *
     * @return 列表
     */
    @GetMapping("/searchAllAnsweredQuestion/")
    public Result searchAllAnsweredQuestion() {
        List<HashMap<String, Object>> result = questionService.searchAllAnsweredQuestion();
        return new Result(result, 200, "成功");
    }

    /**
     * 查找指定专家总回答数和采纳数
     *
     * @param expertId 专家id
     * @return 列表
     */
    @GetMapping("/searchExpertAnswerNumberAndAcceptedNumber/")
    public Result searchExpertAnswerNumberAndAcceptedNumber(@RequestParam String expertId) {
        ExpertInfoVo result = questionService.searchExpertAnswerNumberAndAcceptedNumber(expertId);
        return new Result(result, 200, "成功");
    }

    /**
     * 农户标记回答为最佳回答（或采纳该回答）
     *
     * @return 状态码
     */
    @PostMapping("/acceptedAnswer/")
    public Result acceptedAnswer(@RequestParam("answerId") Integer answerId) {
        Integer result = questionService.addAcceptedAnswer(answerId);
        if (result == 1) {
            return new Result(200, "成功");
        }
        return new Result(404, "失败");
    }

    @GetMapping("/searchQuestion/")
    public Result searchQuestion(@RequestParam String questionId) {
        List<HashMap<String, Object>> result = questionService.searchQuestion(questionId);

        if (result.isEmpty()) {
            return new Result(null, 404, "未找到该问题");
        }

        HashMap<String, Object> firstEntry = result.get(0);

        Map<String, Object> questionMap = new HashMap<>();
        questionMap.put("question_id", firstEntry.get("question_id"));
        questionMap.put("title", firstEntry.get("title"));
        questionMap.put("image", firstEntry.get("image"));
        questionMap.put("question_content", firstEntry.get("question_content"));
        questionMap.put("question_user", firstEntry.get("question_user"));
        questionMap.put("question_user_avatar", firstEntry.get("question_user_avatar"));
        questionMap.put("question_username", firstEntry.get("question_username"));
        questionMap.put("question_time", firstEntry.get("question_time"));

        // 收集所有回答
        List<Map<String, Object>> answers = result.stream().map(entry -> {
            Map<String, Object> answer = new HashMap<>();
            answer.put("answer_id", entry.get("answer_id"));
            answer.put("answer_content", entry.get("answer_content"));
            answer.put("answer_time", entry.get("answer_time"));
            answer.put("answer_user", entry.get("answer_user"));
            answer.put("answer_user_avatar", entry.get("answer_user_avatar"));
            answer.put("answer_username", entry.get("answer_username"));
            answer.put("is_accepted", entry.get("is_accepted"));
            return answer;
        }).collect(Collectors.toList());

        questionMap.put("answers", answers);

        return new Result(questionMap, 200, "成功");
    }

    /**
     * 查找所有已经回答过的问题
     *
     * @return 列表
     */
    @GetMapping("/searchAllUnAnsweredQuestion/")
    public Result searchAllUnAnsweredQuestion() {
        List<HashMap<String, Object>> result = questionService.searchUnAnsweredQuestion();
        return new Result(result, 200, "成功");
    }

    @GetMapping("/searchAllPublicQuestion/")
    public Result searchAllPublicQuestion() {
        List<HashMap<String, Object>> result = questionService.searchAllPublicQuestion();
        return new Result(result, 200, "成功");
    }

    @GetMapping("/searchAllPublicAnsweredQuestion/")
    public Result searchAllPublicAnsweredQuestion() {
        List<HashMap<String, Object>> result = questionService.searchAllPublicAnsweredQuestion();
        return new Result(result, 200, "成功");
    }

    @GetMapping("/searchAllPublicUnAnsweredQuestion/")
    public Result searchAllPublicUnAnsweredQuestion() {
        List<HashMap<String, Object>> result = questionService.searchAllPublicUnAnsweredQuestion();
        return new Result(result, 200, "成功");
    }
}
